import { Controller } from 'egg';
// import { Op } from 'sequelize';
export default class ArticleController extends Controller {
  async index() {
    const { ctx, service } = this;
    const { author, title, type, startTime, endTime } = ctx.request.query;
    const result = await service.article.list({
      author,
      title,
      type,
      startTime,
      endTime,
    });
    ctx.ok(result);
  }

  // 获取文章详情
  async getArticle() {
    const { ctx, service } = this;
    const { articleId } = ctx.params;
    const article = await service.article.getInfo(articleId);
    if (!article) ctx.fail('文章不存在', 404001)
    ctx.ok(article);
  }

  // 网站前台获取文章详情
  async getFrontArticle() {
    const { ctx, service } = this;
    const articleId = ctx.params.articleId;
    const userId = ctx.userId;
    let isFollow: boolean = false;
    let isCollect: boolean = false;
    let isLike: boolean = false;
    const article = await service.article.getInfo(articleId);
    if (!article) {
      ctx.fail('文章不存在', 404001)
      return;
    }
    if (userId) {
      const articleLike = await ctx.model.ArticleLike.findOne({
        where: {
          articleId,
          userId,
          status: 1,
        },
      });
      const articleCollection = await ctx.model.ArticleCollection.findOne({
        where: {
          articleId,
          userId,
          status: 1,
        },
      });
      const follower = await ctx.model.Follower.findOne({
        where: {
          userId: article.authorId,
          followerId: userId,
        },
      });
      if (articleLike) {
        isLike = true;
      }
      if (articleCollection) {
        isCollect = true;
      }
      if (follower) {
        isFollow = true;
      }
    }
    await service.article.updateCount(articleId, 'pvCount', true);
    article.pvCount++;
    ctx.ok({
      article,
      isFollow,
      isCollect,
      isLike,
    });
  }
  // 点赞与取消
  async toggleLike() {
    const { ctx, service } = this;
    const articleId = ctx.params.articleId;
    const userId = ctx.userId;
    const result = await service.article.toggleLike(articleId, userId);
    ctx.ok(result);
  }
  // 获取文章点赞用户
  async getLikeUsers() {
    const { ctx, service } = this;
    const articleId = Number(ctx.params.articleId);
    const result = await service.article.getLikeUsersByArticleId(articleId);
    ctx.ok(result);
  }
  // 文章收藏
  async toggleCollect() {
    const { ctx, service } = this;
    const articleId = ctx.params.articleId;
    const userId = ctx.userId;
    const result = await service.article.toggleCollect(articleId, userId);
    ctx.ok(result);
  }
  // 新增或更新
  async save() {
    const { ctx, service } = this;
    const author = ctx.userId;
    const payload = ctx.request.body;
    let articleId = payload.id;
    const draftId = payload.draftId;
    if (articleId) {        // 更新
      const userArticle = await ctx.model.Article.findOne({
        id: articleId,
        authorId: author,
      });
      if (!userArticle) {
        ctx.fail('文章不存在', 404001)
        return;
      }
      await service.article.doUpdate(articleId, payload);
    } else {          // 新增
      payload.authorId = author;
      const newArticle = await service.article.doSave(payload);
      articleId = newArticle.id;
    }
    if (draftId) {
      await service.draft.remove(draftId, author, articleId);
    }
    ctx.ok(null, '保存成功');
  }
  // 获取分类和标签
  async getCategoriesAndTags() {
    const { ctx, config } = this;
    const categories = config.categories;
    ctx.ok({
      categories,
    });
  }
}
